package com.adcc.automation.gui.Controller;

import com.adcc.automation.gui.base.ExtendedAnimatedFlowContainer;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.FlowHandler;
import io.datafx.controller.flow.container.ContainerAnimations;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.Objects;

@ViewController(value = "/fxml/app.fxml", title = "首页")
public class AppController {
    private static final Logger logger = LoggerFactory.getLogger(AppController.class);
    
    @FXMLViewFlowContext
    private ViewFlowContext context;
    @FXML
    private StackPane dialog_container;
    @FXML
    private JFXDialog dialog;
    @FXML
    private JFXDialogLayout dialog_content;
    @FXML
    private StackPane dialog_box;
    @FXML
    private StackPane content;
    
    @PostConstruct
    public void initUI() throws Exception {
        Objects.requireNonNull(context);
        //注册控制类到上下文
        context.register("appView", this);
        Flow innerFlow = new Flow(MainController.class);
        final FlowHandler flowHandler = innerFlow.createHandler(context);
        context.register("ContentFlowHandler", flowHandler);
        context.register("ContentFlow", innerFlow);
        final Duration containerAnimationDuration = Duration.millis(320);
        content.getChildren().add(flowHandler.start(new ExtendedAnimatedFlowContainer(containerAnimationDuration, ContainerAnimations.SWIPE_LEFT)));
        content.setPrefWidth(Screen.getPrimary().getBounds().getWidth());
        context.register("content", content);
        
        //注册dialog节点
        context.register("dialog_container", dialog_container);
        context.register("dialog", dialog);
        dialog.getChildren().get(0).getStyleClass().clear();
        dialog.getChildren().get(0).setStyle("-fx-background-radius: 5px;-fx-border-radius: 5px");
        dialog.setDialogContainer(dialog_container);
        //注册dialog 内容
        Flow dialogFlow = new Flow(MessageController.class);
        final FlowHandler dialogFlowHandler = dialogFlow.createHandler(context);
        bindNodeToController("alert", MessageController.class, dialogFlow);
        context.register("dialogFlowHandler", dialogFlowHandler);
        dialog_content.getChildren().clear();
        dialog_content.getChildren().add(dialogFlowHandler.start());
        dialog_box.setPrefWidth(Screen.getPrimary().getBounds().getWidth());
    }
    
    public void close() {
        JFXDialog dialog = (JFXDialog) context.getRegisteredObject("dialog");
        FlowHandler dialogFlowHandler = (FlowHandler) context.getRegisteredObject("dialogFlowHandler");
        try {
            dialogFlowHandler.handle("alert");
        } catch (Exception ex) {
            logger.error("Failed to open close stage! {}", ex.getMessage());
        }
        dialog.setTransitionType(JFXDialog.DialogTransition.CENTER);
        dialog.setOverlayClose(false);
        dialog.show();
        MessageController messageController = (MessageController) context.getRegisteredObject("alert");
        messageController.updateContent("确认退出程序？");
        messageController.confirm.setOnAction(new EventHandler<>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                dialog.close();
                Platform.exit();
                logger.info("App exit successfully!");
                System.exit(-1);
            }
        });
        messageController.close.setOnAction(new EventHandler<>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                dialog.close();
            }
        });
    }
    
    //消息提示
    public void showMessage(String message) {
        Platform.runLater(() -> {
            JFXDialog dialog = (JFXDialog) context.getRegisteredObject("dialog");
            FlowHandler dialogFlowHandler = (FlowHandler) context.getRegisteredObject("dialogFlowHandler");
            try {
                dialogFlowHandler.handle("alert");
            } catch (Exception ex) {
                logger.error("Failed to open message stage! {}", ex.getMessage());
            }
            dialog.setTransitionType(JFXDialog.DialogTransition.CENTER);
            dialog.setOverlayClose(false);
            dialog.show();
            MessageController alertController = (MessageController) context.getRegisteredObject("alert");
            alertController.updateContent(message);
            alertController.confirm.setOnAction(new EventHandler<>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    dialog.close();
                }
            });
            alertController.close.setOnAction(new EventHandler<>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    dialog.close();
                }
            });
        });
    }
    
    //页面路由
    private void bindNodeToController(String id, Class<?> controllerClass, Flow flow) {
        flow.withGlobalLink(id, controllerClass);
    }
}